//
//  CQTabBar.m
//  01-OC微博
//
//  Created by 陈乾 on 15/10/21.
//  Copyright © 2015年 陈乾. All rights reserved.
//

#import "CQTabBar.h"
#import "CQTabBarButton.h"

@interface CQTabBar()

@property (nonatomic, weak) UIButton  *plusBtn;

@property (nonatomic, strong) CQTabBarButton *selectedBtn;

@property (nonatomic, strong) NSMutableArray *buttonArr;




@end

@implementation CQTabBar

-(NSMutableArray *)buttonArr
{
    if (_buttonArr == nil) {
        _buttonArr = [NSMutableArray array];
        
    }
    return _buttonArr;
    
}

#pragma mark -   外界的入口 给itemArr赋值 创建按钮 添加按钮监听时间
- (void)setItemArr:(NSArray *)itemArr
{
    _itemArr = itemArr;
    
    for (UITabBarItem *item in _itemArr) {
        
        //创建我自己的按钮
        CQTabBarButton *btn = [CQTabBarButton buttonWithType:UIButtonTypeCustom];
        
        //把每一个item 传给按钮
        btn.item = item;
        
        //设置tag
        btn.tag = self.buttonArr.count;
        
        //选中第一个item
        if (btn.tag == 0) {
            [self btnSelected:btn];
        }
        
        //自己管理选中状态
        [btn addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchDown];
        
        //添加到数组
        [self.buttonArr addObject:btn];
        
        //添加到tabBar
        [self addSubview:btn];
        
    }
    
}

- (void)btnSelected:(CQTabBarButton*)btn
{
    self.selectedBtn.selected = NO;
    self.selectedBtn = btn;
    self.selectedBtn.selected = YES;
    
    //代理
    if ([self.delegate respondsToSelector:@selector(tabBar:andTag:)]) {
        [self.delegate tabBar:self andTag:btn.tag];
    }
    
}

/**
 *  加号按钮的懒加载
 */
-(UIButton *)plusBtn
{
    if (_plusBtn == nil) {
        UIButton *btn = [[UIButton alloc] init];
        
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        //不要设置frame 自己根据背景 或图片 文字的大小 适应
        
        [btn sizeToFit];
        
        _plusBtn = btn;
        
        [self addSubview:btn];
    }
    
    return _plusBtn;
}

/**
 *  加号按钮的布局
 */
-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat w = self.bounds.size.width;
    CGFloat h = self.bounds.size.height;
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat btnW = w /(self.itemArr.count+1);
    CGFloat btnH = h;
    int i = 0;
    for (CQTabBarButton *tabBarBtn in self.buttonArr)
    {
        if (i == 2)
        {
            i = 3;
        }
        btnX = btnW*i;
        tabBarBtn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        i++;
    }
    //把加号按钮固定在中间就ok
    self.plusBtn.center = CGPointMake(w*0.5, h*0.5);
    
}

@end
